//
//  Hiker.swift
//  SwiftUi
//
//  Created by xm on 30/08/2025.
//

import Foundation
import Combine
import SwiftUI

struct Hike: Codable,Hashable, Identifiable{
    var name:String
    var id: Int
    var distance: Double
    var difficulty: Int
    var observations: [Observation]
    
    static var formatter = LengthFormatter()
    
    var distanceText:String{
        return Hike.formatter.string(fromValue: distance, unit: .kilometer)
    }
    
    struct Observation:Codable, Hashable {
        var distanceFromStart: Double
        var elevation: Range<Double>
        var pace: Range<Double>
        var heartRate: Range<Double>
    }
    
}




let hikes:[Hike] = load("hikeData.json")


func load<T: Decodable>(_ filename:String, as type: T.Type = T.self) -> T{
    
    let data: Data
    guard let file = Bundle.main.url(forResource: filename, withExtension: nil) else{
        fatalError("在MainBundle里找不到\(filename)")
    }
    
    do {
        data = try Data(contentsOf: file)
    }catch {
        fatalError("Data 错误\(error)")

    }
    
    print("XXX data:", data.count)
    
    do {
        let decoder = JSONDecoder()
        return try decoder.decode(T.self, from: data)
    }catch{
        fatalError("JSONDecoder 错误\(error)")
    }
}
